package 算法练习;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;

public class Jul26 {

    @Test
    public void test5(){
        Solution1 test = new Solution1();
        System.out.println(Arrays.toString(test.twoSum(new int[]{3, 3}, 6)));
    }
}

class Solution1 {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> hash = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            int sub = target - nums[i];
            if (hash.containsKey(sub)){
                return new int[]{i,hash.get(sub)};
            }
        }
        throw new IllegalArgumentException("未找到");
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) p = p.next;
            if (q != null) q = q.next;
        }
        if (carry > 0) {
            curr.next = new ListNode(carry);
        }
        return dummyHead.next;
    }

}


